package com.whity.towerdefense.controler.states;

import java.io.IOException;
import java.util.Vector;

import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SlidingDrawer;

import com.whity.towerdefense.R;
import com.whity.towerdefense.TowerDefenseActivity;
import com.whity.towerdefense.Utils;
import com.whity.towerdefense.controler.GameControler;
import com.whity.towerdefense.controler.I_State;
import com.whity.towerdefense.model.GameModel;
import com.whity.towerdefense.model.Map;
import com.whity.towerdefense.model.Tower;
import com.whity.towerdefense.model.TowerFactory;
import com.whity.towerdefense.model.TowerType;
import com.whity.towerdefense.view.GameView;
import com.whity.towerdefense.view.GraphicElt;

public class DragNewTowerAP implements I_State
{
	// =============================================================
	// Private attributs :
	
	private static final String TAG = "com.whity.towerdefense.controler.states";
	
	private TowerType		m_TowerType;
	private Tower			m_TransparentTower;
	
	private	SlidingDrawer	m_TowerPanel;
	private Vector<Button>	m_TowerButtons;
	private ImageView		m_TowerHandle;
	
	// =============================================================
	// Public functions :
	
	public DragNewTowerAP(TowerType p_TowerType)
	{
		m_TowerType = p_TowerType;
		m_TowerPanel = (SlidingDrawer)TowerDefenseActivity.ACTIVITY.findViewById(R.id.TowerSlider);
		m_TowerHandle = (ImageView)TowerDefenseActivity.ACTIVITY.findViewById(R.id.handle);
		m_TowerButtons = new Vector<Button>();
		
		Vector<String> towerIDs = GameModel.LEVEL.GetBaseTowers();
		for(int i = 0 ; i < towerIDs.size() ; i++)
		{
			Button towerButton;
			if(i == 0)
			{
				towerButton = (Button)TowerDefenseActivity.ACTIVITY.findViewById(R.id.Tower01);
			}
			else if(i == 1)
			{
				towerButton = (Button)TowerDefenseActivity.ACTIVITY.findViewById(R.id.Tower02);
			}
			else if(i == 2)
			{
				towerButton = (Button)TowerDefenseActivity.ACTIVITY.findViewById(R.id.Tower03);
			}
			else if(i == 3)
			{
				towerButton = (Button)TowerDefenseActivity.ACTIVITY.findViewById(R.id.Tower04);
			}
			else
			{
				continue;
			}
			m_TowerButtons.add(towerButton);
		}
	}
	
	public void OnChangeState()
	{
		//m_TowerPanel.animateClose();
		//m_TowerPanel.bringToFront();
		//m_TowerHandle.setImageResource(R.drawable.right_arrow);
		
		m_TransparentTower = m_TowerType.CreateTower(0, 0);
		m_TransparentTower.SetAlpha(0.4f);
		m_TransparentTower.DisplayRange(true);
		GameModel.LEVEL.GetMap().AddTower(m_TransparentTower);
	}

	public void OnClickEvent(View p_View)
	{
		
	}
	
	public void OnDrawerOpenEvent()
	{
		m_TowerPanel.bringToFront();
		m_TowerHandle.setImageResource(R.drawable.left_arrow);
	}
	
	public void OnDrawerCloseEvent()
	{
		m_TowerPanel.bringToFront();
		m_TowerHandle.setImageResource(R.drawable.right_arrow);
	}
	
	public void OnPause()
	{
		
	}
	
	public void OnResume()
	{
		
	}
	
	public boolean OnMotionEvent(View p_View, MotionEvent p_Evt)
	{
		if(p_Evt.getAction() == MotionEvent.ACTION_MOVE)
		{
			float x = p_Evt.getRawX();
			float y = p_Evt.getRawY();
			PointF mapPos = new PointF(p_Evt.getRawX(), p_Evt.getRawY());
			Utils.ScreenToMap(mapPos);
			
			if(mapPos.x > 0 && mapPos.y > 0)
			{
				mapPos.x = (float) (Math.floor(mapPos.x) + 0.5);
				mapPos.y = (float) (Math.floor(mapPos.y) + 0.5);
			}
			
			m_TransparentTower.SetPosition(mapPos.x,  mapPos.y);
			return true;
		}
		else if(p_Evt.getAction() == MotionEvent.ACTION_UP)
		{
			GameModel.LEVEL.GetMap().RemoveTower(m_TransparentTower);
			GameModel.LEVEL.AddTower(m_TransparentTower);
			m_TransparentTower.SetAlpha(1.0f);
			m_TransparentTower.DisplayRange(false);
			GameControler.SetState(new IdleAP());
			return true;
		}
		else
		{
			return false;
		}
	}
	
	// =============================================================
}
